﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using CDFWPUG.Model;
using WP7Contrib.Collections;
using System.Linq;

namespace CDFWPUG.Helpers
{
    public class NewsHelper
    {
        public static ObservableCollection<FeedItem> Test_GetNewsFeed()
        {
            ObservableCollection<FeedItem> list = new ObservableCollection<FeedItem>();
            var resource = System.Windows.Application.GetResourceStream(new Uri("Data/News.xml", UriKind.Relative));
            StreamReader streamReader = new StreamReader(resource.Stream);
            string xml = streamReader.ReadToEnd();
            XElement xmlApps = XElement.Parse(xml);

            var items = from item in xmlApps.Descendants("item")
                        select new FeedItem
                        {
                            Title = item.Element("title").Value,
                            Guid = item.Element("guid").Value,
                            Description = item.Element("description").Value,
                            Link = item.Element("link").Value,
                            Published = Convert.ToDateTime(item.Element("pubDate").Value)
                        };


            list = new ObservableCollection<FeedItem>(items);
            return list;


        }


        public static ObservableCollection<FeedItem> ParseXML(String xml)
        {


            XElement xmlApps = XElement.Parse(xml);

            var items = from item in xmlApps.Descendants("item")
                        select new FeedItem
                        {
                            Title = item.Element("title").Value,
                            Guid = item.Element("guid").Value,
                            Description = item.Element("description").Value,
                            Link = item.Element("link").Value,
                            Published = Convert.ToDateTime(item.Element("pubDate").Value)
                        };

            return new ObservableCollection<FeedItem>(items);


        }


        public static ObservableCollection<FeedItem> GetNewsFromIsolatedStorage()
        {
            string xml = Helpers.IsolatedStorageHelper.LoadFile("NewsItems.xml");
            var results = ParseXML(xml);
            return results;
        }

    }
}
